<script>
	import pub from './common/js/pub.js'
	import amap from '@/common/js/amap-wx.js'
	export default {
		onLaunch: function(options) {
			console.log('App Launch')
			// H5已移入/pages/index处理
			// #ifndef H5
			uni.removeStorageSync("secret");
			uni.removeStorageSync("appid");
			uni.removeStorageSync("openid");
			uni.removeStorageSync("account");
			uni.removeStorageSync("userinfo");
			uni.removeStorageSync("headimage");
			uni.removeStorageSync("user");
			uni.removeStorageSync("origin");
			// #endif
			this.amap = new amap.AMapWX({
				key:"b56e180ab79c37ab0452a550917e74fc"
			})
			//不移除，保留上次的当前城市
			// uni.removeStorageSync("localcity")
			uni.removeStorageSync("isPublishing")
			//不移除，保留上次的城市列表
			// uni.removeStorageSync("cities")
			uni.removeStorageSync("log")

			let city = this.pub.getLocalcity()
			if (!city || city == "") {
				this.pub.setLocalcity("兰州市")
			}

			//#ifdef MP-WEIXIN
			//一部手机游甘肃
			this.pub.setAppid("wx21fe321ff1e3e2e0")
			this.pub.setSecret("399a00cb038da7f934128c1af9489cb8")
			// this.login()
			//#endif

			// this.pub.getSwitcher();
			// this.pub.getCityList()
			// this.getLocation()
			// this.getLocationInfo();
			// this.getLocalcity("22.53332", "113.93041");
		},
		onShow: function() {
			console.log('App Show')

		},
		onHide: function() {
			console.log('App Hide')
			uni.removeStorageSync("log")
		},
		methods: {
			login() {
				var that = this
				let wx_appid = this.pub.getAppid()
				let wx_secret = this.pub.getSecret()
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						// console.log("登录信息-------------------->",loginRes)
						that.$postApi(that.$path.GET_OPENID_WX, {
							code: loginRes.code, //要去换取openid的登录凭证
							channelType: '1'
						}).then(data => {
							console.log("登录信息-------------------->",data)
							that.pub.setOpenid(data.data.openid)
							that.pub.setAccount(data.data.openid)
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log("用户信息-------------->",infoRes)
									that.nickName = infoRes.userInfo.nickName
									that.avatarUrl = infoRes.userInfo.avatarUrl
									var params = {
										openid:that.pub.getOpenid(),
										nickname:that.nickName,
										headPortrait:that.avatarUrl,
										channelType:1
									}
									that.$postApi(that.$path.SET_NEW_VIP, params).then(res => {
										console.log("会员登录返回结果-------------------------->",res.data)
										let memberId = res.data.data.id
										that.pub.setMemberId(memberId)
									})
								},
								fail:function(info){
									console.log("获取用户信息失败-------->",info)
								}
							})
						})
					}
				})
			},
			getLocation:function(){
				this.amap.getRegeo({
					success:data=>{
						console.log("高德返回--------->",data[0])
						let result = data[0]
						this.latitude = result.latitude
						this.longitude = result.longitude
						this.address = result.regeocodeData
						console.log("地址信息----------->",this.address)
						this.pub.setLocalcity(this.address.addressComponent.city)
					}
				})
			},
			getLocationInfo() {
				let that = this;
				// #ifdef MP-WEIXIN
				uni.getSetting({
					success: res => {
						if (!res.authSetting['scope.userLocation']) {
							uni.authorize({
								scope: 'scope.userLocation',
								success() {
									uni.getLocation({
										type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
										success(res) {
											const latitude = res.latitude
											const longitude = res.longitude
											that.getLocalcity(latitude, longitude)
										}
									})
								},
								fail() {

								}
							})
						} else {
							uni.getLocation({
								type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
								success(res) {
									const latitude = res.latitude
									const longitude = res.longitude
									that.getLocalcity(latitude, longitude)
								}
							})
						}
					},
					fail: res => {
						console.log(res)
					}
				})
				//#endif
				//#ifdef H5
				uni.getLocation({
					type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
					success(res) {
						const latitude = res.latitude
						const longitude = res.longitude
						that.getLocalcity(latitude, longitude)
					},
					complete: function() {},
					fail: function(res) {
						that.pub.setLocalcity("兰州市")
					}
				})
				//#endif
			},
			getLocalcity(latitude, longitude) {
				let that = this 
				let key = "b549bb4575d2e34c6a3373d6f4fa5467";
				let url = "https://restapi.amap.com/v3/geocode/regeo";
				let location = longitude+","+latitude;
				let param = {
					key: key,
					location:location
				}
				this.$getApi(url,param).then(res=>{
					let data = res.data;
					let city = data.regeocode.addressComponent.city||"兰州市"
					let province = data.regeocode.addressComponent.province;
					if(province!="甘肃省") {
						city = "兰州市";
					}
					that.pub.setLocalcity(city);
				})
			},
		}
	}
</script>

<style lang="scss">
	.scenceIndex,
	.search,
	.city,
	.my {
		font-size: 30upx;
	}

	.color888 {
		color: #888888;
	}

	.fw-bold {
		font-weight: bold;
	}

	.fs-30 {
		font-size: 30upx;
	}

	.fs-28 {
		font-size: 28upx;
	}

	.fs-24 {
		font-size: 24upx;
	}

	.fs-20 {
		font-size: 20upx;
	}

	.fs-15 {
		font-size: 15upx;
	}


@font-face {
  font-family: 'iconfont';  /* project id 1174594 */
  src: url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.eot');
  src: url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.eot?#iefix') format('embedded-opentype'),
  url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.woff2') format('woff2'),
  url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.woff') format('woff'),
  url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.ttf') format('truetype'),
  url('//at.alicdn.com/t/font_1174594_rcsrwizbnck.svg#iconfont') format('svg');
}
	@import "./components/colorui/main.css";
	@import "./components/colorui/icon.css";
	@import url("./common/css/main.scss");
	@import url("./common/css/iconfont.css");
	@import "./components/gaoyia-parse/parse.css";
	// @import url("https://at.alicdn.com/t/font_1174594_a9ebty437hf.css")
	/*每个页面公共css */
</style>
